package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.SysDocumentCodeRule;
import com.ruoyi.system.service.ISysDocumentCodeRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 单据编码规则维护
 *
 * @author wn
 * @date 2024/8/14
 */
@RestController
@Slf4j
public class SysDocumentCodeRuleController extends BaseController {

    @Resource
    private ISysDocumentCodeRuleService documentRuleService;

    /**
     * 列表
     */
    @PreAuthorize("@ss.hasPermi('system:documentrule:list')")
    @PostMapping(name = "单据编码规则列表", path = "/system/document/list")
    public TableDataInfo list(@RequestBody SysDocumentCodeRule documentRuleRequest) {
        startPage();
        List<SysDocumentCodeRule> list = documentRuleService.getPageList(documentRuleRequest);
        return getDataTable(list);
    }

    /**
     * 根据id查询
     */
    @PreAuthorize("@ss.hasPermi('system:documentrule:query')")
    @GetMapping(name = "根据id查询单据编码规则", path = "/system/document/getById")
    public AjaxResult getById(@RequestParam Long id) {
        SysDocumentCodeRule documentRule = documentRuleService.getById(id);
        return success(documentRule);
    }

    /**
     * 保存
     */
    @PreAuthorize("@ss.hasPermi('system:documentrule:add')")
    @PostMapping(name = "保存单据编码规则", path = "/system/document/save")
    public AjaxResult save(@RequestBody SysDocumentCodeRule documentRuleRequest) {
        SysDocumentCodeRule documentRule = documentRuleService.getByCode(documentRuleRequest.getDocumentCode());
        if(documentRule != null){
            return error("单据编码代码已存在");
        }
        documentRuleRequest.setCreateBy(getUsername());
        documentRuleRequest.setCreateTime(new Date());
        documentRuleRequest.setOrgId(getDeptId());
        documentRuleService.saveDocumentRule(documentRuleRequest);
        return success();
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:documentrule:edit')")
    @PostMapping(name = "修改单据编码规则", path = "/system/document/update")
    public AjaxResult update(@RequestBody SysDocumentCodeRule documentRuleRequest) {
        SysDocumentCodeRule documentRule = documentRuleService.getByCode(documentRuleRequest.getDocumentCode());
        if(documentRule != null && !Objects.equals(documentRule.getId(), documentRuleRequest.getId())){
            return error("单据编码已存在");
        }
        documentRuleRequest.setUpdateBy(getUsername());
        documentRuleRequest.setUpdateTime(new Date());
        documentRuleService.updateDocumentRule(documentRuleRequest);
        return success();
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:documentrule:remove')")
    @PostMapping(name = "删除单据编码规则", path = "/system/document/delete")
    public AjaxResult delete(@RequestParam("ids") String ids) {
        documentRuleService.deleteCurrency(ids);
        return success();
    }


    /**
     * 获取单据编码
     */
    @GetMapping(name = "获取单据编码",path = "/system/document/code")
    public AjaxResult getDocumentCodeByCode(@RequestParam("code") String code, @RequestParam("length") Integer length){
        List<String> s = documentRuleService.getDocumentCodeByCode(code,length);
        if(s == null){
            return error("单据不存在");
        }
        return success(s);
    }
    /**
     * 关键字查询
     */
    @GetMapping(name = "关键字查询", path = "/system/document/condition")
    public AjaxResult condition() {
        return success(documentRuleService.getSelectCondition());
    }
}
